package com.user.notiy.common.utils;

import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.user.notiy.UmsStringUtils;



public class NoticeList {
	public static final long MAX_DEPTH = 1000;
	public static final long EXPIRE = 30 * 1000;
	
	private volatile static NoticeList uniueqNoticeList;
	private static Map<String, ConcurrentLinkedQueue<Notcie>> lists = new Hashtable<String, ConcurrentLinkedQueue<Notcie>>();
	
	private NoticeList() {}
	
	/**
	 * @Func	init
	 * @Brief	单例构造
	 */
	public static void init() {
		/* 单例判断 */
		if(uniueqNoticeList == null) {
			synchronized (NoticeList.class) {
				if(uniueqNoticeList == null) {
					uniueqNoticeList = new NoticeList();
				}
			}
		}
	}
	
	/**
	 * @Func	createList
	 * @Brief	创建队列
	 */
	public static void createList(String type) {
		createList(type, null);
	}
	
	/**
	 * @Func	createList
	 * @Brief	创建队列
	 */
	public static void createList(String type, String node) {
		init();
		
		/* 如果不存在则创建 */
		if(!uniueqNoticeList.isExists(type, node)) {
			uniueqNoticeList.getLists().put(getName(type, node), new ConcurrentLinkedQueue<Notcie>());
		}
	}
	
	/**
	 * @Func	add
	 * @Brief	添加通知
	 */
	public static void add(String type, String node, Map<String, String> map) {
		init();
		
		/* 获取队列 */
		ConcurrentLinkedQueue<Notcie> list = null;
		list = uniueqNoticeList.getList(type, node);
		if(list == null) {
			list = uniueqNoticeList.getList(type, null);
		}
		
		/* 初始化通知结构体 */
		Notcie notcie = new Notcie(map);

		/* 添加到队列 */
		list.add(notcie);
	}
	
	/**
	 * @Func	next
	 * @Brief	获取下一条通知
	 */
	public static Map<String, String> next(String type, String node) {
		init();

		/* 获取队列 */
		ConcurrentLinkedQueue<Notcie> list = uniueqNoticeList.getList(type, node);
		
		/* 获取通知信息 */
		while(list != null) {
			Notcie notcie = list.poll();
			if(notcie == null) {
				return null;
			} else {
				if((System.currentTimeMillis() - notcie.getCheckInTime()) < EXPIRE) {
					return notcie.getData();
				}
			}
		}
		
		return null;
	}
	
	/**
	 * @Func	getInfo
	 * @Brief	获取队列信息
	 */
	public static String getInfo() {
		init();

		/* 拼写信息 */
		StringBuffer str = new StringBuffer();
		str.append("通知队列：");
		for(Map.Entry<String, ConcurrentLinkedQueue<Notcie>> list : uniueqNoticeList.getLists().entrySet()) {
			str.append("\n  [" + list.getKey() + "]队列剩余[" + list.getValue().size() + "]笔");
		}
		
		return str.toString();		
	}
	
	/**
	 * @Func	clean
	 * @Brief	清理指定队列
	 */
	public static void clean(String type, String node) {
		init();

		/* 节点的队列是否存在 */
		if(uniueqNoticeList.isExists(type, node)) {
			uniueqNoticeList.getList(type, node).clear();
		}
	}
	
	/**
	 * @Func	clean
	 * @Brief	清理所有堆积队列
	 */
	public static void clean() {
		init();
	
		for(Map.Entry<String, ConcurrentLinkedQueue<Notcie>> list : uniueqNoticeList.getLists().entrySet()) {
			if(list.getValue().size() > MAX_DEPTH) {
				list.getValue().clear();				
			}
		}
	}
	
	/**
	 * @Func	getLists
	 * @Brief	获取所有队列
	 */
	private Map<String, ConcurrentLinkedQueue<Notcie>> getLists() {
		return lists;
	}

	/**
	 * @Func	getList
	 * @Brief	获取队列
	 */
	private ConcurrentLinkedQueue<Notcie> getList(String type, String node) {
		ConcurrentLinkedQueue<Notcie> list = null;
		
		if(isExists(type, node)) {
			list = lists.get(getName(type, node));
		}
		
		return list;
	}
	
	/**
	 * @Func	isExists
	 * @Brief	是否存在
	 */
	private boolean isExists(String type, String node) {
		return lists.containsKey(getName(type, node));
	}
	
	/**
	 * @Func	getName
	 * @Brief	获取名称
	 */
	private static String getName(String type, String node) {
		if(UmsStringUtils.isNull(node)) {
			return type;
		} else {
			return type + "." + node;
		}
	}
}